package com.tbit.main.controller;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.AndWeather;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.pojo.Geo;
import com.tbit.main.pojo.andWeather.AndWeatherNow;
import com.tbit.main.service.GeoService;
import com.tbit.main.service.RedisService;
import com.tbit.main.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时天气
 *
 * @author LMY
 * @create 2020-11-18 17:40
 */
@RestController
@RequestMapping("/weather")
public class WeatherNewController {

    @Autowired
    private GeoService geoService;
    @Autowired
    private RedisService redisService;

    /**
     * 获取
     *
     * @param token
     * @param mapType
     * @param accountId
     * @return
     */
    @RequestMapping("/new")
    public JsonResult getWeatherNew(String token, Integer mapType, Integer accountId) {

        //天气信息
        AndWeatherNow andWeatherNow = new AndWeatherNow();

        try {
            //时间value不能为空，时间不能超过1h，数据value不能为空
            if (redisService.get(RedisConstant.REDIS_WEATHER_NEW_DATE + accountId) == null
                    || (System.currentTimeMillis() - Long.valueOf(redisService.get(RedisConstant.REDIS_WEATHER_NEW_DATE + accountId))) > 1000 * 60 * 60
                    || redisService.get(RedisConstant.REDIS_WEATHER_NEW + accountId) == null
            ) {
                List<Geo> geos = geoService.getByAccountId(accountId);
                if (geos.size() > 0) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("url", AndWeather.business);
                    map.put("location", geos.get(0).getCenterLo() + "," + geos.get(0).getCenterLa());
                    map.put("key", SysConstant.WEATHER_KEY);
                    String data = HttpClientUtil.httpGet(HttpClientUtil.urlJoint(map));
                    if (data != null) {
                        andWeatherNow = JSONObject.parseObject(data, AndWeatherNow.class);
                        if (andWeatherNow.getCode().equals("200")) {
                            redisService.add(RedisConstant.REDIS_WEATHER_NEW + accountId, data, 4200);
                            redisService.add(RedisConstant.REDIS_WEATHER_NEW_DATE + accountId, String.valueOf(System.currentTimeMillis()), 4200);
                        } else {
                            andWeatherNow = JSONObject.parseObject(redisService.get(RedisConstant.REDIS_WEATHER_NEW + accountId), AndWeatherNow.class);
                            redisService.add(RedisConstant.REDIS_WEATHER_NEW_DATE + accountId, String.valueOf(System.currentTimeMillis()), 4200);
                        }
                    } else {
                        andWeatherNow = JSONObject.parseObject(redisService.get(RedisConstant.REDIS_WEATHER_NEW + accountId), AndWeatherNow.class);
                        redisService.add(RedisConstant.REDIS_WEATHER_NEW_DATE + accountId, String.valueOf(System.currentTimeMillis()), 4200);
                    }
                }
            } else {
                String weather = redisService.get(RedisConstant.REDIS_WEATHER_NEW + accountId);
                try {
                    andWeatherNow = JSONObject.parseObject(weather, AndWeatherNow.class);
                } catch (JSONException error) {
                    redisService.del(RedisConstant.REDIS_WEATHER_NEW + accountId);
                }

            }
        } catch (Exception e) {
            redisService.del(RedisConstant.REDIS_WEATHER_NEW_DATE + accountId);
        }

        return JsonResult.succ(andWeatherNow);
    }
}
